package
{
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.RadioButton;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	
	/**
	 * Block Generator Class
	 * 
	 * Creates blocks for different section of a given class
	 * TODO: currently upto 5 sections total
	 * 
	 * by Team CLAC 
	 * Front End Division 11/8/08
	 */
	public class BlockGenerator
	{

		/* Maximum number of section */
		private var maxNumSection:int = 5;

		/* color for current class */
		private var currColor:uint = 0x0;
		
		/* name of current class */
		private var currName:String = "";

		/* number of sections for this class */
		private var numSection:int = 0;
		
		/* Array of radio button for each section */
		private var radioButtons  :Array = new Array();
		
		/* Section information */
		private var dayInfo       :Array = new Array(maxNumSection);
		private var startIndexInfo:Array = new Array(maxNumSection);
		private var durationInfo  :Array = new Array(maxNumSection);

		/* Last box selected */
		private var lastBox:UIComponent = null;
		
		/* Radio groupt of current class */
		private var sectionGroup:Canvas = null;
		
		/* Class Manager object that actually created current object */
		private var manager:ClassManager = null;
		
		/* Fade Effects */
		private var fadeOut:Fade = new Fade();
		private var fadeIn :Fade = new Fade();
		/**
		 * Constructor
		 */  
		public function BlockGenerator(color:uint, name:String, man:ClassManager)
		{
			currColor  = color;
			currName   = name;
			numSection = 0;
			manager    = man;
			
			sectionGroup = new Canvas();
			sectionGroup.label = name;
			
			fadeOut = new Fade();
			fadeOut.alphaFrom = 1.0;
			fadeOut.alphaTo   = 0;
			fadeOut.duration  = 500;
			
			fadeIn  = new Fade();
			fadeIn.alphaFrom = 0;
			fadeIn.alphaTo   = 1.0;
			fadeIn.duration  = 500;
		}
		
		/**
		 * adds section
		 */ 
		public function addSection(day:int, startIndex:int, duration:int):void
		{
			// if number of section is aready at max, stop adding 
			if(numSection >= maxNumSection)
				return;
				
			dayInfo       [numSection] = day;
			startIndexInfo[numSection] = startIndex;
			durationInfo  [numSection] = duration;

			//manager.mesh.add

			var currRadio:RadioButton = new RadioButton();
			currRadio.x = 10;
			currRadio.y = 10 + (numSection) * 20;
			currRadio.label     = getNameSection(numSection);
			currRadio.enabled   = true;
			currRadio.groupName = currName;
			currRadio.addEventListener(MouseEvent.CLICK, makeBox);
			
			sectionGroup.addChild(currRadio);
			radioButtons[numSection] = currRadio;
			
			numSection++;
		}
		
		/**
		 * Function responsible for making, displaying classes box
		 */
		public function makeBox(evt:MouseEvent):void {
			
			// figure out which radio is selected. If error occurs, just return
			var indexRadio:int = selectedRadio();
			if(indexRadio == -1)
				return;
			
			// remove last box created from display
			if(lastBox) {
				lastBox.setStyle("hideEffect", fadeOut);
				lastBox.visible = false;
				//manager.grid.removeChild(lastBox);
			}
			
			// create new box for selected section
        	var box:UIComponent = lastBox = new UIComponent();
        	manager.grid.addChild(box);
        	
        	
        	
        	box.setStyle("showEffect", fadeIn);
        	box.stylesInitialized();
        	box.graphics.beginFill(currColor, 1);
            box.graphics.drawRoundRect( 71 + (dayInfo[indexRadio]-1) * 103, 92 + (startIndexInfo[indexRadio]*41), 102, 41 * durationInfo[indexRadio],15,15 );
            box.graphics.endFill();
          
            //Alert.show();
		}
		
		/**
		 * Find out which radio button is selected
		 */
		public function selectedRadio():int {
			
			for(var i:int = 0; i<numSection; i++) {
				if(RadioButton(radioButtons[i]).selected)
					return i;
			}
			return -1;
		}
		
		/**
		 * Adds the radio group of the class
		 */
		public function addSectionGroup(group:Canvas):void {
			sectionGroup = group;
		}
		
		/**
		 * Returns the radio group of the class
		 */
		public function getSectionGroup():Canvas{
			return sectionGroup;
		}
		
		/**
		 * Returns the name of the class
		 */
		public function getClassName():String {
			return currName;
		}
		
		/**
		 * Returns the number of sections
		 */
		public function getNumSection():int {
			return numSection;
		}
		
		/**
		 * Returns the section name
		 */
		public function getNameSection(index:int):String {
			var retVal:String = "";
			
			switch(dayInfo[index]) {
				case 1: retVal = "Monday ";
						break;
				case 2: retVal = "Tuesday ";
						break;
				case 3: retVal = "Wednesday ";
						break;
				case 4: retVal = "Thursday ";
						break;
				case 5: retVal = "Friday ";
						break;		
				default:retVal = " ";
						break;
			}
			
			retVal += "- "
			
			var startHour:int = startIndexInfo[index]/2;
			retVal += (10 + startHour) + ":";
			
			if(startIndexInfo[index]%2 == 0)
				retVal += "00 ";
			else
				retVal += "30 ";
	 
			retVal += "- "
			
			startHour = (startIndexInfo[index]+durationInfo[index])/2;
			retVal += (10 + startHour) + ":";
			
			if((startIndexInfo[index]+durationInfo[index])%2 == 0)
				retVal += "00 ";
			else
				retVal += "30 ";
			
			return retVal;
		}
	}
}